home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / WWW / http / www.cu-amiga.co.uk / features / c-tutorial / Part-13.lzx / Part-13 / setf0.c < prev    next >
C/C++ Source or Header  |  1987-05-04  |  4KB  |  166 lines

  1. #include<exec/libraries.h>
  2. #include<graphics/text.h>
  3. #include<intuition/intuition.h>
  4. #include<intuition/screens.h>
  5. #include<libraries/gadtools.h>
  6. #include<utility/tagitem.h>
  7.  
  8. #include<string.h>
  9. #include<stdio.h>
  10.  
  11. #include<clib/exec_protos.h>
  12. #include<clib/gadtools_protos.h>
  13. #include<clib/intuition_protos.h>
  14.  
  15. /* The library base global variables */
  16. struct Library* IntuitionBase;
  17. struct Library* GadToolsBase;
  18.  
  19. /* Need to give prototypes for our functions */
  20. void handleIDCMP(struct Window*);
  21. void setupWindow();
  22. void createWindow(struct Gadget*);
  23.  
  24. #define MYFONTSIZE        (8)
  25.  
  26. /* Some constants for the size of the window */
  27. #define MYWIN_WIDTH        (400)
  28. #define MYWIN_HEIGHT    (200)
  29.  
  30. /* Some constants for the position and size of our gadget */
  31. #define MYGAD_LEFT        (10)
  32. #define MYGAD_TOP            (10+MYFONTSIZE)
  33. #define MYGAD_WIDTH        (MYWIN_WIDTH-MYGAD_LEFT*2)
  34. #define MYGAD_HEIGHT    (MYWIN_HEIGHT-MYGAD_TOP*2+MYFONTSIZE)
  35. #define MYGAD_TEXT        ("My ListView")
  36. #define MYGAD_ID            (0)
  37.  
  38. /* Initialised structure declaration: describes standard Topaz font */
  39. static struct TextAttr topazFont = { "topaz.font", MYFONTSIZE, 0, 0, };
  40.  
  41. /* The start of the program */
  42. void main()
  43. {
  44.     /* Open libraries... */
  45.     if(IntuitionBase = OpenLibrary("intuition.library",37))
  46.     {
  47.         if(GadToolsBase = OpenLibrary("gadtools.library",37))
  48.         {
  49.             /* Now do the real work */
  50.             setupWindow();
  51.             CloseLibrary(GadToolsBase);
  52.         }
  53.         else
  54.             printf("Error: could not open gadtools.library\n");
  55.         CloseLibrary(IntuitionBase);
  56.     }
  57.     else
  58.         printf("Error: could not open intuition.library\n");
  59. }
  60.  
  61. /* Setup the window -- do the GadTools stuff */
  62. void setupWindow()
  63. {
  64.     struct Screen* scr;
  65.     /* We'll copy the visual information for the default public screen */
  66.   /* (usually, this is the Workbench screen) */
  67.     if(scr = LockPubScreen(NULL))
  68.     {
  69.         APTR vinfo;
  70.         /* Get the visual info so GadTools can render the gadgets nicely */
  71.         if(vinfo = GetVisualInfo(scr, TAG_DONE))
  72.         {
  73.             struct Gadget* glist;
  74.             struct Gadget* gad;
  75.             /* Start a GadTools gadget list */
  76.             glist = NULL;
  77.             if(gad = CreateContext(&glist))
  78.             {
  79.                 int offtop, offleft;
  80.                 struct NewGadget newgad;
  81.                 /* The offsets of our window borders */
  82.                 offleft = scr->WBorLeft;
  83.                 offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
  84.                 /* Setup our first gadget */
  85.                 newgad.ng_TextAttr         = &topazFont;
  86.                 newgad.ng_VisualInfo     = vinfo;
  87.                 newgad.ng_LeftEdge         = MYGAD_LEFT + offleft;
  88.                 newgad.ng_TopEdge         = MYGAD_TOP + offtop;
  89.                 newgad.ng_Width             = MYGAD_WIDTH;
  90.                 newgad.ng_Height             = MYGAD_HEIGHT;
  91.                 newgad.ng_GadgetText    = MYGAD_TEXT;
  92.                 newgad.ng_GadgetID        = MYGAD_ID;
  93.                 newgad.ng_Flags                = 0;
  94.                 /* Now create it and add it to our list */
  95.                 if(gad = CreateGadget(LISTVIEW_KIND, gad, &newgad, TAG_DONE))
  96.                     createWindow(glist);
  97.                 else
  98.                     printf("Error: could not create gadget(s)\n");
  99.                 /* Free the gadget */
  100.                 FreeGadgets(glist);
  101.             }
  102.             else
  103.                 printf("Error: could not create GadTools context\n");
  104.             FreeVisualInfo(vinfo);
  105.         }
  106.         else
  107.             printf("Error: could not get visual info\n");
  108.         UnlockPubScreen(NULL, scr);
  109.     }
  110.     else
  111.         printf("Error: could not lock public screen\n");
  112. }
  113.  
  114. /* Actually open the window, in the normal way */
  115. void createWindow(struct Gadget* glist)
  116. {
  117.     struct Window* win;
  118.     /* Open our window */
  119.     if(win = OpenWindowTags(NULL,
  120.                                                     WA_InnerWidth,    MYWIN_WIDTH,
  121.                                                     WA_InnerHeight,    MYWIN_HEIGHT,
  122.                                                     WA_Flags,        WFLG_CLOSEGADGET | WFLG_DRAGBAR,
  123.                                                     WA_IDCMP,        IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  124.                                                     WA_Gadgets,    glist,
  125.                                                     TAG_DONE,        0))
  126.     {
  127.         /* Let GadTools refresh its bits of the window */
  128.         GT_RefreshWindow(win, NULL);
  129.         /* Now handle messages */
  130.         handleIDCMP(win);
  131.         CloseWindow(win);
  132.     }
  133.     else
  134.         printf("Error: could not open window\n");
  135. }
  136.  
  137. /* Our message handling code */
  138. void handleIDCMP(struct Window* win)
  139. {
  140.     int going = TRUE;
  141.     while(going)
  142.     {
  143.         struct IntuiMessage* intuimsg;
  144.         /* Wait for messages to arrive */
  145.         WaitPort(win->UserPort);
  146.         /* Messages have arrived: loop through all of them */
  147.         while(intuimsg = GT_GetIMsg(win->UserPort))
  148.         {
  149.             /* Act on this message... */
  150.             switch(intuimsg->Class)
  151.             {
  152.             case IDCMP_CLOSEWINDOW:
  153.                 going = FALSE;
  154.                 break;
  155.             case IDCMP_REFRESHWINDOW:
  156.                 /* You *MUST* remember to ask for and handle these refresh messages */
  157.                 GT_BeginRefresh(win);
  158.                 GT_EndRefresh(win, TRUE);
  159.                 break;
  160.             }
  161.             /* Reply when finished with message */
  162.             GT_ReplyIMsg(intuimsg);
  163.         }
  164.     }
  165. }
  166.